<template>
	<view class="login-container">
		<!-- 登陆图标 -->
		<uni-icons type="contact-filled" size="100" color="#afafaf"></uni-icons>
		<!-- 登陆按钮 -->
		<button type="primary" class="btn-login" @click="getUserProfile">一键登录</button>
		<!-- 登陆提示 -->
		<view class="tip_text">
			登陆后享更多权益
		</view>
	</view>
</template>

<script>
	import {mapMutations,mapState} from 'vuex'
	export default {
		name:"my-login",
		data() {
			return {
				
			};
		},
		methods:{
			...mapMutations('m_user',['updateUserInfo','updateRedirectInfo']),
			getUserProfile(){
				uni.getUserProfile({
				desc: '你的授权信息',
				success: (res) => {
				// 将信息存到 vuex 中
					this.updateUserInfo(res.userInfo)
					this.getToken(res)
				},
				fail: (res) => {
					return uni.$showMsg('您取消了登录授权')
				}
			})
			},
			async getToken(info){
				const  res = await uni.login().catch(err => err)
				if(!res || res.errMsg !== 'login:ok') return uni.$showMsg("登陆失败")
				// console.log(res.code);
				// console.log(info);
				const query = {
					code : res.code,
					encryptedData : info.encryptedData,
					rawData : info.rawData,
					signature : info.signature,
					iv : info.iv
				}
				const resp = await uni.$http.post("/api/public/v1/users/wxlogin",query)
				// console.log(resp);
				if(resp.data.meta.state !==200) return this.successLogin()
				return this.successLogin()
			},
			successLogin(){
				uni.$showMsg("登录成功")
				console.log(this.redirectInfo);
				uni.switchTab({
					url:this.redirectInfo.from,
					complete:()=> {
						this.redirectInfo = null
					}
				})
			}
		},
		computed:{
			...mapState('m_user',['redirectInfo'])
		}
	}
</script>

<style lang="scss" scoped>
.login-container{
	height: 750rpx;
	background-color: #f8f8f8;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	position: relative;
	overflow: hidden;
	&::after{
		content: " ";
		display: block;
		width: 100%;
		height: 40px;
		background-color: white;
		position: absolute;
		bottom: 0;
		left: 0;
		border-radius: 100%;
		transform: translateY(50%);
	}
	.btn-login{
		width: 50%;
		border-radius: 100px;
		margin: 15px 0;
		background-color: #c00;
	}
	.tip_text{
		font-size: 12px;
		color:grey;
	}
}
</style>